/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.ui.controls {
	import com.proyecto404.core.EventArgs;
	import com.proyecto404.core.exception.InvalidOperationException;
	import com.proyecto404.core.ui.BaseMovieClip;
	
	import flash.events.MouseEvent;
	
	import org.osflash.signals.ISignal;
	import org.osflash.signals.Signal;
	
	public class CheckBox extends BaseMovieClip {
		private var _checked:Boolean;
		private var _enabled:Boolean;
		private var _changed:Signal;
		
		public function CheckBox() {
			super();
			stop();
			
			if (!labels.containsKey("on")) {
				throw new InvalidOperationException("CheckBox must contain a frame with 'on' label");
			}
			
			if (!labels.containsKey("off")) {
				throw new InvalidOperationException("CheckBox must contain a frame with 'off' label");
			}
			
			if (!labels.containsKey("on-disabled")) {
				throw new InvalidOperationException("CheckBox must contain a frame with 'on-disabled' label");
			}
			
			if (!labels.containsKey("off-disabled")) {
				throw new InvalidOperationException("CheckBox must contain a frame with 'off-disabled' label");
			}
			
			click.add(onClick);
			_checked = false;
			_enabled = true;
			mouseChildren = true;
			buttonMode = true;
			_changed = new Signal(EventArgs);
			gotoAndStop("off");
		}
		
		private function onClick(e:MouseEvent):void {
			if (enabled) {
				_checked = !_checked;
				updateStatus();
				_changed.dispatch(new EventArgs());
			}
		}
		
		private function updateStatus():void {
			if (_enabled) {
				if (_checked) {
					gotoAndStop("on");
				} else {
					gotoAndStop("off");
				}
			} else {
				if (_checked) {
					gotoAndStop("on-disabled");
				} else {
					gotoAndStop("off-disabled");
				}
			}
		}
		
		public function get checked():Boolean {
			return _checked;
		}
		
		public function get changed():ISignal {
			return _changed;
		}
		
		public override function get enabled():Boolean {
			return _enabled;
		}
		
		public function enable():void {
			_enabled = true;
			mouseChildren = true;
			updateStatus();
		}
		
		public function disable():void {
			_enabled = false;
			mouseChildren = false;
			updateStatus();
		}

	}
}